<?php


namespace app\admin\controller;
use PHPExcel;

class MoneyShopping extends Common
{
    public function index()
    {
        if (input('get.keywords')) {
            $this->map[] = ['us_id', '=', input('get.keywords')];
        }
        if (input('get.start') && input('get.end') == "") {
            $this->map[] = ['add_time', '>=', input('get.start')];
        }
        if (input('get.start') == "" && input('get.end')) {
            $this->map[] = ['add_time', '<=', input('get.end')];
        }
        if (input('get.start') && input('get.end')) {
            $this->map[] = ['add_time', 'between', array(input('get.start'), input('get.end'))];
        }
        $this->map[] = ['type','=',1];
        $list = model('MoneyDetailed')
            ->where($this->map)
            ->order('add_time', 'desc')
            ->paginate(10, false, $config = ['query' => request()->param()]);

        foreach ($list as $k => $v) {
            $list[$k]['us_nickname'] = model('user')->where('id', $v['us_id'])->value('us_nickname');
            $list[$k]['ShoppingName'] = model('order_detail')->where('or_id', $v['or_id'])->value('or_pd_name');
        }
        $money = model('MoneyDetailed')->where($this->map)
            ->order('add_time', 'desc')
            ->sum('money');
        $this->assign(['list' => $list, 'money' => $money]);
        return $this->fetch();
    }

    public function excel(){//导出Excel

        $start = input('get.time_start');
        $end = input('get.time_send');
        $start_time = date("Y-m-d H:i:s",$start/1000);
        $end_time =$end!='NaN'? date("Y-m-d H:i:s",$end/1000):date('Y-m-d H:i:s');

        if (input('get.keywords')) {
            $this->map[] = ['us_id', 'like', '%'.input('get.keywords').'%'];
        }

        if ($start && $start == "") {
            $this->map[] = ['add_time', '>=', $start_time];
        }
        if ($start == "" && $end) {
            $this->map[] = ['add_time', '<=', $end_time];
        }
        if ($start && $end) {
            $this->map[] = ['add_time', 'between', array($start_time, $end_time)];
        }
        $this->map[] = ['type','=',1];

        $info = model('MoneyDetailed')
            ->where($this->map)
            ->select();

        foreach ($info as $k => $v) {
            $info[$k]['us_nickname'] = model('user')->where('id', $v['us_id'])->value('us_nickname');
            $info[$k]['us_tel'] = model('user')->where('id', $v['us_id'])->value('us_tel');
        }
//        halt(Db::name('OrderDetail')->getLastSql());

        $xlsName  = "购买记录";
        $xlsCell  = array(
            array('or_num', '订单号'),
            array('money', '商品价格'),
            array('us_nickname', '用户昵称'),
            array('us_tel', '电话'),
            array('add_time', '时间'),
        );

        $xlsData  = array();

        foreach ($info as $k => $v) {
            $xlsData[$k]['or_num'] = $v['or_num'];
            $xlsData[$k]['money'] = $v['money'];
            $xlsData[$k]['us_nickname'] = $v['us_nickname'];
            $xlsData[$k]['us_tel'] = $v['us_tel'];
            $xlsData[$k]['add_time'] = $v['add_time'];
        }

        $this->exportExcel($xlsName,$xlsCell,$xlsData);

    }

    function exportExcel($expTitle,$expCellName,$expTableData){
        //include_once EXTEND_PATH.'PHPExcel/PHPExcel.php';//方法二
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        $num = $dataNum + 3;
        $objPHPExcel = new PHPExcel();//方法一
        //$objPHPExcel = new \PHPExcel();//方法二
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->getActiveSheet(0)->mergeCells('A'.$num.':'.$cellName[$cellNum-1].$num);//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  表格生成时间:'.date('Y-m-d H:i:s'));
        // halt($num);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$num, '制表人:                      ' . '会计：                      ' . '出纳：                      ' . '审核：                      ');
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        ob_end_clean();//这一步非常关键，用来清除缓冲区防止导出的excel乱码
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//"xls"参考下一条备注
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//"Excel2007"生成2007版本的xlsx，"Excel5"生成2003版本的xls
        $objWriter->save('php://output');
        exit;
    }

}